package com.skwen.delivery.superdelivery.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.skwen.delivery.superdelivery.BaseActivity;
import com.skwen.delivery.superdelivery.R;
import com.skwen.delivery.superdelivery.login.bean.User;
import com.skwen.delivery.superdelivery.myGreenDao.manager.UserDBManager;
import com.skwen.delivery.superdelivery.myGreenDao.manager.WriteOrderDBManager;
import com.skwen.delivery.superdelivery.order.bean.WriteOrder;
import com.skwen.delivery.superdelivery.util.OidGen;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;



/**
 * Created by Administrator on 2017/11/9.
 * 填写
 */

public class WriteOrderActivity extends BaseActivity {


    @BindView(R.id.left_button)
    ImageButton mLeftButton;
    @BindView(R.id.title_name)
    TextView mTitleName;
    @BindView(R.id.right_text)
    TextView mRightText;
    @BindView(R.id.order_address)
    TextInputEditText mOrderAddress;
    @BindView(R.id.send_name)
    TextInputEditText mSendName;
    @BindView(R.id.send_phone)
    TextInputEditText mSendPhone;
    @BindView(R.id.goods_type)
    TextView mGoodsType;
    @BindView(R.id.goods_weight)
    TextView mGoodsWeight;
    @BindView(R.id.remark)
    TextInputEditText mRemark;
    @BindView(R.id.cash_on_delivery)
    CheckBox mCashOnDelivery;
    @BindView(R.id.go_on_add)
    TextView mGoOnAdd;
    @BindView(R.id.submit_good)
    TextView mSubmitGood;
    @BindView(R.id.receive_name)
    TextInputEditText mReceiveName;
    @BindView(R.id.receive_phone)
    TextInputEditText mReceivePhone;
    @BindView(R.id.change_address)
    ImageView mChangeAddress;
    @BindView(R.id.change_receive_address)
    ImageView mChangeReceiveAddress;
    @BindView(R.id.order_receive_address)
    TextInputEditText mOrderReceiveAddress;

    private WriteOrderDBManager dbManager;
    private String [] typeList;
    private String [] weightList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_order_layout);
        ButterKnife.bind(this);
        initViews();
        freshUI();
    }

    private void initViews() {
        dbManager = WriteOrderDBManager.getInstance(this);
        String address = getIntent().getStringExtra("address");
        mTitleName.setText("填写订单");
        mOrderAddress.setText(address);
    }

    private void freshUI() {
        Observable.just("skwen")
                .subscribeOn(Schedulers.newThread())
                .map(s -> {
                    typeList = getResources().getStringArray(R.array.goods_type);
                    weightList = getResources().getStringArray(R.array.goods_weight);
                    UserDBManager userDbManager = UserDBManager.getInstance(WriteOrderActivity.this);
                    List<User> list = userDbManager.queryUserList();
                    User user = null;
                    if (list != null && list.size() > 0) {
                        user = list.get(0);
                    }
                    return user;
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(user -> {
                    if (user != null) {
                        mSendName.setText(getIfNullText(user.getUserName()));
                        mSendPhone.setText(getIfNullText(user.getUserPhone()));
                    }

                });


    }

    @OnClick({R.id.left_button, R.id.goods_type, R.id.goods_weight, R.id.cash_on_delivery, R.id.go_on_add, R.id.submit_good, R.id.change_address, R.id.change_receive_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_button:
                dbManager.clearWriteOrder();
                finish();
                break;
            case R.id.change_address://修改发货地址
                break;
            case R.id.change_receive_address://修改收货地址
                break;
            case R.id.goods_type://选择物品类型
                showSingleAlertDialog(mGoodsType,"选择物品类型",typeList);
                break;
            case R.id.goods_weight://选择物品重量范围
                showSingleAlertDialog(mGoodsWeight,"选择物品重量",weightList);
                break;
            case R.id.cash_on_delivery://货到付款
                break;
            case R.id.go_on_add://继续添加
                /**
                 * 步骤1 保存本页数据到本地数据库
                 * 步骤2 清空保存本页数据继续添加
                 */
                ifNullData();
                saveToLocal();
                clearPageData();

                break;
            case R.id.submit_good://提交订单
                /**
                 * 步骤1 查询本地数据库是否有订单 如果有 一起提交
                 */
                ifNullData();
                saveToLocal();
                List<WriteOrder> writeOrderList = dbManager.queryWriteOrderList();
                if (writeOrderList != null && writeOrderList.size() > 0) {
                    showConfirmDialog("订单提交", "确认提交吗？", position -> {
                        /**
                         * 提交订单到服务器处理
                         *  查看提交的订单
                         */
                        showToast(mSubmitGood,"订单提交服务器");
                        startActivity(new Intent(WriteOrderActivity.this,WriteOrderDetialActivity.class));
                        finish();

                    });
                } else {
                    showToast(mSubmitGood, "订单信息为空");
                    return;
                }
                break;
        }
    }

    private void ifNullData() {
        if (TextUtils.isEmpty(mOrderAddress.getText())) {
            showToast(mOrderAddress, "发件地址不能为空");
            return;
        }

        if (TextUtils.isEmpty(mSendName.getText())) {
            showToast(mSendName, "发件人姓名不能为空");
            return;
        }
        if (TextUtils.isEmpty(mSendPhone.getText())) {
            showToast(mSendPhone, "发件人电话不能为空");
            return;
        }

        if (TextUtils.isEmpty(mOrderReceiveAddress.getText())) {
            showToast(mOrderReceiveAddress, "收件地址不能为空");
            return;
        }

        if (TextUtils.isEmpty(mReceiveName.getText())) {
            showToast(mReceiveName, "收件人姓名不能为空");
            return;
        }

        if (TextUtils.isEmpty(mReceivePhone.getText())) {
            showToast(mReceivePhone, "收件人电话不能为空");
            return;
        }

        if (TextUtils.isEmpty(mRemark.getText())) {
            showToast(mRemark, "物品描述不能为空");
            return;
        }

        if (TextUtils.isEmpty(mGoodsType.getText())) {
            showToast(mGoodsType, "物品类型不能为空");
            return;
        }

        if (TextUtils.isEmpty(mGoodsWeight.getText())) {
            showToast(mGoodsWeight, "物品重量不能为空");
            return;
        }
    }


    private void saveToLocal() {

        WriteOrder writeOrder = new WriteOrder();
        writeOrder.setId(OidGen.getInstance().getId());
        writeOrder.setSendAddress(getIfNullText(mOrderAddress.getText()));
        writeOrder.setSendName(mSendName.getText().toString());
        writeOrder.setSendPhone(mSendPhone.getText().toString());
        writeOrder.setReceiveAddress(mOrderReceiveAddress.getText().toString());
        writeOrder.setReceiveName(mReceiveName.getText().toString());
        writeOrder.setReceicePhone(mReceivePhone.getText().toString());
        writeOrder.setGoodsRemark(mRemark.getText().toString());
        writeOrder.setGoodsType(mGoodsType.getText().toString());
        writeOrder.setGoodsWeight(mGoodsWeight.getText().toString());

        dbManager.insertWriteOrder(writeOrder);

    }


    private void clearPageData() {

        mOrderReceiveAddress.setText("");
        mOrderReceiveAddress.setHint("请输入收件地址");

        mReceiveName.setText("");
        mReceiveName.setHint("请输入收件人姓名");

        mReceivePhone.setText("");
        mReceivePhone.setHint("请输入收件人电话");

        mRemark.setText("");
        mRemark.setHint("请输入物品备注信息");

        mGoodsType.setText("");
        mGoodsType.setHint("物品类型");

        mGoodsWeight.setText("");
        mGoodsWeight.setHint("物品重量");

    }

}
